﻿using AGVController.BaseClass;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace UYD_WCS_AS_AGV_Controller.AGV
{
    /// <summary>
    /// AGV上报的状态管理
    /// </summary>
    public class AgvStatusManager
    {
        private readonly Dictionary<string, AgvStatusObservable<object>> _AllAgvStatus = new Dictionary<string, AgvStatusObservable<object>>();

        public AgvStatusManager()
        {
            // AGV上报的状态
            for (int i = 0; i < Common.Robot_Push_included_fields.Count; i++)
            {
                string varName = Common.Robot_Push_included_fields[i];
                var variable = new AgvStatusObservable<object>(varName, 0);
                // 默认触发条件：新值<>原值 变化
                variable.TriggerCondition = (oldVal, newVal) => oldVal != newVal;
                _AllAgvStatus[varName] = variable;
            }
        }

        // 动态设置单个状态变化的触发条件
        public void SetTriggerCondition(string varName, Func<object, object, bool> condition)
        {
            if (_AllAgvStatus.TryGetValue(varName, out var variable))
            {
                variable.TriggerCondition = condition;
            }
        }

        // 注册全局回调
        public void RegisterGlobalCallback(Action<string, object, object> callback)
        {
            foreach (var variable in _AllAgvStatus.Values)
            {
                variable.ValueChangedCallback = (name, oldVal, newVal) =>
                {
                    callback(name, oldVal, newVal);
                };
            }
        }

        // 获取变量对象（用于数据绑定）
        public AgvStatusObservable<object> GetObservableAgvStatus(string varName) =>
            _AllAgvStatus.TryGetValue(varName, out var variable) ? variable : null;
    }
}
